/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    My_Ringtone
 *    
 *    ActionTypeDialog
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-1-20
 *    @version: 1.0
 *
 ******************************************************************************/

package com.tools.ringtone.view;

import android.app.Dialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;

import com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.R;
import com.tools.ringtone.data.MusicFileItem;

/**
 * ActionTypeDialog of My_Ringtone.
 * 
 * @author zuochen
 * 
 */

public class ActionTypeDialog extends Dialog implements
		android.view.View.OnClickListener {
	private final Context mContext;
	private final MusicFileItem musicFileItem;

	/**
	 * Initiate a new instance of {@link ActionTypeDialog}.
	 * 
	 * @param context
	 */
	public ActionTypeDialog(Context context, MusicFileItem aMusicFileItem) {
		super(context);
		this.mContext = context;
		this.musicFileItem = aMusicFileItem;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.action_type_dialog);

		findViewById(R.id.button_play).setOnClickListener(this);
		findViewById(R.id.button_set_as).setOnClickListener(this);
		findViewById(R.id.button_edit).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_play:
			final Uri uri = Uri.parse("file://" + musicFileItem.getPath());
			play(uri);
			break;

		case R.id.button_set_as:
			dismiss();
			final RingtoneTypeDialog ringtoneTypeDialog = new RingtoneTypeDialog(
					mContext, ContentUris.withAppendedId(
							MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
							(musicFileItem.getId())));
			ringtoneTypeDialog.show();
			break;
		case R.id.button_edit:
			com.mp3.dwonloader.ringtonemaker.musicradio.version.zl.activity.AudioEditActivity
					.startEdit(mContext, Uri.parse(musicFileItem.getPath()));
			dismiss();
			break;
		}
	}

	private void play(Uri uri) {
		final Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "audio/*");
		// intent.addCategory(Intent.CATEGORY_BROWSABLE);
		mContext.startActivity(intent);
		dismiss();
	}
}
